require "event_editor/io"
require "map_editor/io"
module MapEditor
  class MapContainer < Gtk::VBox
    attr_reader :map_panel, :zoom_index, :panel_mode, :current_layer_no, :show_layers
    def initialize(main_container)
      super
      self.sensitive = false
      @map_panel = nil
      @main_container = main_container

      @tool_bar = self.create_toolbar
      @table = Gtk::Table.new(1, 2)  
      @table.attach(@tool_bar, 0, 1, 1, 2, Gtk::EXPAND | Gtk::FILL, 0)

      @zoom_index = 2
      @panel_mode = :map_drawer
      @current_layer_no = 0
      @show_layers = true
      
      self.add(@table)
    end

    def clear
      if @map_panel
        @table.remove(@map_panel)
      end
      @map_panel = nil
      self.sensitive = false
    end
    
    def map_panel=(map_panel)
      self.clear
      @map_panel = map_panel
      @table.attach(map_panel, 0, 1, 2, 3, Gtk::EXPAND | Gtk::FILL, Gtk::EXPAND | Gtk::FILL)
      self.sensitive = true
    end

    # Pen tool
    def create_toolbar
      toolbar = Gtk::Toolbar.new
      tool_buttons = {}      

      b = add_tool_toggle_button(tool_buttons, toolbar, "icon/layer_bottom.xpm", "Bottom Layer", :layer){
        @current_layer_no = 0
        @show_layers = true
        @main_container.map_panel.current_layer_no = 0
        @main_container.side_bar.page = 0
        @main_container.map_panel.panel_mode = :map_drawer
        @panel_mode = @main_container.map_panel.panel_mode
        @main_container.map_panel.show_layers = true
      }
      b.active = true

      add_tool_toggle_button(tool_buttons, toolbar, "icon/layer_middle.xpm", "Middle Layer", :layer){
        @current_layer_no = 1
        @show_layers = true
        @main_container.map_panel.current_layer_no = 1
        @main_container.side_bar.page = 0
        @main_container.map_panel.panel_mode = :map_drawer
        @panel_mode = @main_container.map_panel.panel_mode
        @main_container.map_panel.show_layers = true
      }
      
      # add_tool_toggle_button(tool_buttons, toolbar, "icon/layer_top.xpm", "Top Layer", :layer){
        # @current_layer_no = 2
        # @show_layers = true
        # @main_container.map_panel.current_layer_no = 2
        # @main_container.side_bar.page = 0
        # @main_container.map_panel.panel_mode = :map_drawer
        # @panel_mode = @main_container.map_panel.panel_mode
        # @main_container.map_panel.show_layers = true
      # }

      add_tool_toggle_button(tool_buttons, toolbar, "icon/event.xpm", "event", :layer){
        @main_container.side_bar.page = 1
        @show_layers = false
        @main_container.map_panel.show_layers = false
        @main_container.map_panel.panel_mode = :event_controler
        @panel_mode = @main_container.map_panel.panel_mode
      }
      
      toolbar.append_space
      add_tool_toggle_button(tool_buttons, toolbar, "icon/1_1.xpm", "1_1", :zoom){
        @main_container.map_panel.zoom_index = 3
        @zoom_index = 3
      }

      b = add_tool_toggle_button(tool_buttons, toolbar, "icon/1_2.xpm", "1_2", :zoom){
        @main_container.map_panel.zoom_index = 2
        @zoom_index = 2
      }
      b.active = true
      
      add_tool_toggle_button(tool_buttons, toolbar, "icon/1_4.xpm", "1_4", :zoom){
        @main_container.map_panel.zoom_index = 1
        @zoom_index = 1
      }
      
      add_tool_toggle_button(tool_buttons, toolbar, "icon/1_8.xpm", "1_8", :zoom){
        @main_container.map_panel.zoom_index = 0
        @zoom_index = 0
      }
      
      toolbar.append_space
      
      add_tool_button(toolbar, "Save", "icon/save.xpm")do
          MapEditor::IO.save(@main_container.map_model)
          EventEditor::IO.save(@main_container.event_model)
          @main_container.map_navigator.save
      end
      
      return toolbar
    end
    
    def on_resize(width, height)
      @map_panel.on_resize(width, height - @tool_bar.height_request) if @map_panel
    end
  end
end